package com.designpattern.design.memeto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 备忘录模式
 * @author 15790
 *
 */
public class MemetoTest {
public static void main(String[] args) {
	GameRole gameRole = new GameRole();
	gameRole.setAttack(100);
	gameRole.setDef(100);
	
	gameRole.display();
	System.out.println("大战前存档");
	CareTaker careTaker = new CareTaker();
	careTaker.careTakerMemeto(gameRole.createMemeto());
	gameRole.setAttack(20);
	gameRole.setDef(30);
	System.out.println("========大战后数据");
	gameRole.display();
	System.out.println("开始还原");
	gameRole.recoverMemetoFromCareTaker(careTaker.memeto);
	gameRole.display();
	
}
}

class Memeto{
	private int attack;
	private int def;
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	public int getDef() {
		return def;
	}
	public void setDef(int def) {
		this.def = def;
	}
	
	
}

/**
 * 备忘录对象
 * @author 15790
 *
 */
class CareTaker{
	//单个游戏角色
	Memeto memeto;
	//单个游戏角色多次状态
	List<Memeto> meemtos=new ArrayList<>();
	//多个角色最新状态
	Map<String,Memeto> meemtoMap=new HashMap<>();
	
	
	public CareTaker() {
		super();
		//由于只要保存一个角色，所以只需要初始单个就好了
		memeto=new Memeto();
	}

	//备份数据
	void careTakerMemeto(Memeto memeto) {
		this.memeto=memeto; 
	}
}
/**
 * 游戏角色
 * @author 15790
 *
 */
class GameRole{
	private int attack;
	private int def;
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	public int getDef() {
		return def;
	}
	public void setDef(int def) {
		this.def = def;
	}
	
	public Memeto createMemeto() {
		Memeto memeto=new Memeto();
		memeto.setAttack(attack);
		memeto.setDef(def);
		return memeto;
	}
	public void  recoverMemetoFromCareTaker(Memeto memeto) {
		this.attack=memeto.getAttack();
		this.def=memeto.getDef();
	}
	public void display() {
		System.out.println("当前攻击="+attack+",当前防御="+def);
	}
}